//
//  ColorEditViewController.m
//  笔记1
//
//  Created by  on 2022/11/6.
//

#import "ColorEditViewController.h"
#import "ColorCell.h"

@interface ColorEditViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *array;
@property (assign, nonatomic) NSInteger index;
@end

@implementation ColorEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    for (int i = 0; i< 25; i++) {
        [self.array addObject:[self nextHex:@"#C00000" count:i]];
    }
    [self.collectionView registerNib:[ColorCell nib] forCellWithReuseIdentifier:[ColorCell ID]];
}
- (NSString *)nextHex:(NSString*)hex count:(NSInteger)count{
    NSString *str = [hex substringFromIndex:1];
    NSInteger num  = [str toDemical] - 250000 * count;
    NSString *newnum = [NSString stringWithFormat:@"%ld",num];
    NSString *newhex = [NSString stringWithFormat:@"#%@",[newnum toHex]];
    return newhex;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ColorCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ColorCell ID] forIndexPath:indexPath];
    NSString *color = self.array[indexPath.row];
    NSLog(@">>>>>>>>%@",color);
    cell.colorView.bgHexColor = color;
    cell.cornerRadius = 20;
    cell.borderWidth = 5;
    cell.borderHexColor = self.index == indexPath.row ? color : @"#F8D1B4";
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.colorBlock) {
        self.index = indexPath.row;
        NSString *color = self.array[indexPath.row];
        self.colorBlock(color);
    }
    [collectionView reloadData];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(40, 40);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return (SCREEN_WIDTH - 40 - 200) / 4;
}

@end

